<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12" >
            <el-form-item label="app版本号:" label-width="90px">
              <el-input
                placeholder="请输入App版本号"
                v-model="where.version"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="更新类型:">
              <el-select
                clearable
                v-model="where.update_type"
                placeholder="全部"
                class="ele-fluid"
              >
                <el-option v-for="(update_type,key) in updateTypeToStr"  :key="key" :label="update_type" :value="key"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="开启状态:">
              <el-select
                clearable
                v-model="where.status"
                placeholder="全部"
                class="ele-fluid"
              >
                <el-option v-for="(value,key) in statusArr"  :key="key" :label="value" :value="key"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="12">
            <el-form-item label="创建时间:">
              <el-date-picker
                style="width: 100%"
                v-model="where.create_time"
                type="datetimerange"
                value-format="yyyy-MM-dd HH-mm-ss"
                label-format="yyyy-MM-dd HH-mm-ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:boxappversion:add')">添加
          </el-button>
<!--          <el-button-->
<!--            size="small"-->
<!--            type="danger"-->
<!--            icon="el-icon-delete"-->
<!--            class="ele-btn-icon"-->
<!--            @click="removeBatch"-->
<!--            v-if="permission.includes('sys:boxappversion:dall')">删除-->
<!--          </el-button>-->
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:boxappversion:edit')">修改
          </el-link>

          <el-link
            type="primary"
            :underline="false"
            @click="changeStatus(row)"
          >
            <span v-if="row.status==1">关闭</span>
            <span v-if="row.status==2">开启</span>
          </el-link>
<!--          <el-popconfirm-->
<!--            class="ele-action"-->
<!--            title="确定要删除此项目吗？"-->
<!--            @confirm="remove(row)">-->
<!--            <el-link-->
<!--              type="danger"-->
<!--              slot="reference"-->
<!--              :underline="false"-->
<!--              icon="el-icon-delete"-->
<!--              v-if="permission.includes('sys:boxappversion:delete')">删除-->
<!--            </el-link>-->
<!--          </el-popconfirm>-->
        </template>


      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <boxappversion-edit
      :data="current"
      :visible.sync="showEdit"
      :appNames="appNames"
      :appTypeToStr="appTypeToStr"
      :rangeToStr="rangeToStr"
      :updateTypeToStr="updateTypeToStr"
      :statusArr="statusArr"
      @done="reload"/>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import BoxappversionEdit from './boxappversion-edit.vue';
import {left} from "core-js/internals/array-reduce";
export default {
  name: 'ExamBoxAppVersion',
  // eslint-disable-next-line vue/no-unused-components
  components: {BoxappversionEdit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/boxAppVersion/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'app_id',
          label: '应用名称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          formatter: (row, column, cellValue) => {
            let foundElement = this.appNames.find(item => item.id === cellValue);
            return foundElement.app_name;
          }
        },
        {
          prop: 'type',
          label: '应用类型',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.appTypeToStr[cellValue];
          }
        },
        {
          prop: 'version',
          label: '最新版本',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'code',
          label: '最新版本号',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'range_version',
          label: '版本更新范围',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          formatter: (row) => {
            return this.rangeToStr[row.range] + row.range_version;
          }
        },
        {
          prop: 'url',
          label: '更新URL',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'update_type',
          label: '更新方式',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.updateTypeToStr[cellValue];
          }
        },
        {
          prop: 'content',
          label: '更新内容',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'status',
          label: '状态',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.statusArr[cellValue];
          }
        },
        {
          prop: 'update_date',
          label: '更新时间',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'desc',
          label: '备注',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'create_time',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      //app的数组
      appNames : [],
      appTypeToStr: [],
      rangeToStr: [],
      updateTypeToStr: [],
      statusArr: {
        1: '开启',
        2: '关闭'
      }
    };
  },
  created(){
    this.loadAppNames();
    this.loadAppTypeToStr();
    this.loadRangeToStr();
    this.loadUpdateTypeToStr();
  },
  methods: {
    left,
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/boxAppVersion/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的项目吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/boxAppVersion/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },

    loadAppNames() {
      this.$http.get('/boxApp/index').then(res => {
        if (res.data.code === 0) {
          this.appNames = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },

    loadAppTypeToStr() {
      this.$http.get('/boxAppVersion/getAppTypeToStr').then(res => {
        if (res.data.code === 0) {
          this.appTypeToStr = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    loadRangeToStr() {
      this.$http.get('/boxAppVersion/getRangeToStr').then(res => {
        if (res.data.code === 0) {
          this.rangeToStr = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },

    loadUpdateTypeToStr() {
      this.$http.get('/boxAppVersion/getUpdateTypeToStr').then(res => {
        if (res.data.code === 0) {
          this.updateTypeToStr = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },

    changeStatus(row) {
      const loading = this.$loading({lock: true})
      if (row.status == 1) {
        var newStatus = 2;
      } else {
        newStatus = 1;
      }
      this.$http.post('/boxAppVersion/edit', {id: row.id, status: newStatus}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
  }
}
</script>

<style scoped>
</style>
